import { useEffect, useState } from "react";
import fetchData from "../../utils/ApiFetch";
import FilesContainer from "../FilesContainer/FilesContainer";

const apiUrl = process.env.REACT_APP_API_URL;

function FileList(props) {
    const [files, setFiles] = useState([]);
    const [error, setError] = useState(false);
    useEffect(()=>{
        const fetchFiles = async () => {
            try {
                const resp = await fetchData(`${apiUrl}/file/getSharedFiles?sharedCode=${props.sharedCode}`);
                console.log(resp);
                if (resp.code === 401) {
                    localStorage.removeItem('token')
                } else {
                    setError(false)
                    setFiles(resp.data.files.map((e) => { e.selected = false; return e}))
                }
            } catch (error) {
                console.log(error);
            }
        }
        fetchFiles();
    }, [props.sharedCode]);

    function handleContextMenuOpen() {

    }
    function changeParentId() {
        
    }
    function handleFileNameChange() {
        
    }
    function handleFileSelected() {
        
    }
    return (
        <FilesContainer files={files} error={error} handleContextMenuOpen={handleContextMenuOpen} onClick={changeParentId} handleFileNameChange={handleFileNameChange} handleFileSelected={handleFileSelected} readOnly={true} />
    );
}

export default FileList;